<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/9
 * Time: 9:56
 */
namespace app\admin\controller;
use think\Db;
class Purchase extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($keyword1)&&$keyword1!="")
            {
                $map['uuu1.user_username'] = ['like',"%" . $keyword1 . "%"];
            }
            if(isset($purchase_type)&&$purchase_type!="")
            {
                $map['purchase_type'] = $purchase_type;
            }
            if(isset($purchase_target)&&$purchase_target!="")
            {
                $map['purchase_target'] = $purchase_target;
            }
            $count = Db::table('up_purchase')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.purchase_user_id','left')
                ->join('up_u_user uuu1','uuu1.user_id = a.teacher_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_purchase')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.purchase_user_id','left')
                ->join('up_u_user uuu1','uuu1.user_id = a.teacher_id','left')
                ->where($map)
                ->field('uuu.user_username,uuu.user_phone,uuu.user_id,uuu1.user_username as user_username1,a.*')
                ->order('purchase_time desc')
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){
                $vo['mode'] = '平台币';
                if($vo['purchase_type']==1){//公开课
                    $name = Db::table('up_public_class')->where('public_id',$vo['f_id'])->value('public_name');
                    $vo['purchase_type_name'] = '公开课';
                }elseif ($vo['purchase_type']==2){//题库
                    $name = Db::table('up_question_bank')->where('question_id',$vo['f_id'])->value('question_title');
                    $vo['purchase_type_name'] = '题库';
                }elseif ($vo['purchase_type']==3){//一对一
                    $name = $vo['f_id'];
                    $vo['purchase_type_name'] = '一对一';
                }elseif ($vo['purchase_type']==4){//求助
                    $name = $vo['f_id'];
                    $vo['purchase_type_name'] = '求助';
                }
                $vo['name']=$name;
                if($vo['purchase_target']==1){
                    $vo['purchase_target_name'] = '老师';
                }elseif ($vo['purchase_target']==2){
                    $vo['purchase_target_name'] = '平台';
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }
}